﻿using System;
using System.Web.UI.WebControls;
using Token.Piker.BLL;
using System.Text;
using Token.Piker.Common;

namespace Token.Piker.Web.Admin.Manager
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat("AdminName like '%{0}%' OR RealName like '%{0}%'", txtKeyword.Text.Trim());
            }
            AdminBLL bll = new AdminBLL();
            if (pagerList.CurrentPageIndex == 1)
            {
                gridView.DataSource = bll.GetList(pagerList.PageSize, strWhere.ToString(), "AddTime DESC");
            }
            else
            {
                gridView.DataSource = bll.GetListByPage(strWhere.ToString(), "AddTime DESC", pagerList.StartRecordIndex, pagerList.EndRecordIndex);
            }
            gridView.DataBind();
            pagerList.RecordCount = bll.GetRecordCount(strWhere.ToString());
        }

        protected void ChangeState(object sender, EventArgs e)
        {
            LinkButton lkbChangeState = sender as LinkButton;
            if (lkbChangeState.Text == "摄影部部长")
            {
                MessageBox.Show(this.Page, "您无权进行此操作！");
                return;
            }
            else
            {
                if (Session[GlobalConstants.PIKER_ADMIN] == null)
                {
                    MessageBox.ShowAndRedirect(this.Page, "登录超时，请重新登录！", "~/Admin/Login");
                    return;
                }
                AdminBLL adminBLL = new AdminBLL();
                int id = Convert.ToInt32(lkbChangeState.CommandArgument);
                Token.Piker.Model.Admin admin = adminBLL.GetModel(id);
                if (lkbChangeState.Text == "待审核")
                {
                    admin.Flag = 1;
                }
                else if (lkbChangeState.Text == "已审核")
                {
                    admin.Flag = 3;
                }
                else if (lkbChangeState.Text == "已禁用")
                {
                    admin.Flag = 1;
                }
                adminBLL.Update(admin);
            }
 
            BindData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkbChangeState = e.Row.FindControl("lkbChangeState") as LinkButton;
                if (lkbChangeState.Text == "0")
                {
                    lkbChangeState.Text = "待审核";
                }
                else if (lkbChangeState.Text == "1")
                {
                    lkbChangeState.Text = "已审核";
                }
                else if (lkbChangeState.Text == "2")
                {
                    lkbChangeState.Text = "摄影部部长";
                }
                else if (lkbChangeState.Text == "3")
                {
                    lkbChangeState.Text = "已禁用";
                }
            }
        }

        protected void pagerList_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack && !IsCallback)
            {
                BindData();
            }
        }
    }
}